package com.want.evenchat.model;

import android.content.Context;

import com.want.evenchat.model.bean.UserInfo;
import com.want.evenchat.model.dao.UserAccountDAO;
import com.want.evenchat.model.db.DBManager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Want on 2018/5/3.
 */

public class Model {

    private UserAccountDAO userAccountDAO;


    //数据模型层全局类


    private ExecutorService executorService = Executors.newCachedThreadPool();


    private DBManager dbManager;

    private Context mContext;

    //创建对象
    private static Model model = new Model();

    //私有化构造
    private Model() {

    }

    //获取单例对象
    public static Model getInstance() {
        return model;
    }

    //初始化的方法
    public void init(Context context) {
        mContext = context;

        //创建用户帐号数据库的操作类对象
        userAccountDAO = new UserAccountDAO(mContext);

        //开启全局监听
        EvenListener evenListener = new EvenListener(mContext);
    }

    //获取全局线程池对象
    public ExecutorService getGlobalThreadPool() {
        return executorService;
    }

    //用户登录成功后的处理方法
    public void loginSuccess(UserInfo account) {

        //校验
        if (account == null){
            return;
        }
        if (dbManager != null){
            dbManager.close();
        }
        dbManager = new DBManager(mContext, account.getName());
    }

    public DBManager getDBManager(){
        return dbManager;
    }

    //获取用户帐号数据的操作类对象
    public UserAccountDAO getUserAccountDao() {
        return userAccountDAO;
    }
}
